package com.zmovie.front.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmovie.framework.domain.*;
import com.zmovie.framework.service.*;
import com.zmovie.front.constant.Constants;
import com.zmovie.front.controller.vo.CinemaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zmovie.front.controller
 * @since 2023-12-18 17:19
 */
@Controller
@RequestMapping("/cinema")
@Api(tags = "电影院")
public class CinemaController {
    @Autowired
    private CinemaService cinemaService;
    @Autowired
    private CinemaBrandService cinemaBrandService;
    @Autowired
    private CinemaAreaService cinemaAreaService;
    @Autowired
    private HallTypeService hallTypeService;
    @Autowired
    private HallService hallService;

    /**
     * @param cinema     brandId areaId
     * @param hallTypeId 影厅类别id
     */
    private void renderData(Model model, Cinema cinema, Integer hallTypeId, Integer pageNo, Integer pageSize) {
        //根据品牌、区域分页
        Page<Cinema> cinemaPage = cinemaService.pageCinema(cinema, pageNo, pageSize);
        //保留包含指定影厅类别的影院
        List<Cinema> cinemas = cinemaPage.getRecords().stream().filter(new Predicate<Cinema>() {
            @Override
            public boolean test(Cinema cinema) {
                if (hallTypeId == null) {
                    return true;
                }
                List<Hall> halls = hallService.listHallByCinemaId(cinema.getId());
                for (Hall hall : halls) {
                    if (hallTypeId.equals(hall.getHallTypeId())) {
                        return true;
                    }
                }
                return false;
            }
        }).collect(Collectors.toList());
        cinemaPage.setRecords(cinemas);
        model.addAttribute(Constants.CINEMA_PAGE_KEY, convert(cinemaPage));
        //前端选项
        model.addAttribute(Constants.SHOW_BRAND_ID, cinema.getBrandId() == null ? 0 : cinema.getBrandId());
        model.addAttribute(Constants.SHOW_AREA_ID, cinema.getAreaId() == null ? 0 : cinema.getAreaId());
        model.addAttribute(Constants.SHOW_HALL_TYPE_ID, hallTypeId == null ? 0 : hallTypeId);
    }

    @GetMapping
    @ApiOperation("电影院页面")
    public String index(Model model) {
        //影院品牌
        List<CinemaBrand> cinemaBrands = cinemaBrandService.listCinemaBrands(new CinemaBrand());
        model.addAttribute(Constants.CINEMA_BRANDS_KEY, cinemaBrands);
        //行政区
        List<CinemaArea> cinemaAreas = cinemaAreaService.listCinemaAreas(new CinemaArea());
        model.addAttribute(Constants.CINEMA_AREAS_KEY, cinemaAreas);
        //影厅类型
        List<HallType> hallTypes = hallTypeService.listHallType(new HallType());
        model.addAttribute(Constants.HALL_TYPES_KEY, hallTypes);
        //影院分页列表
        renderData(model, new Cinema(), null, Constants.PAGE_NO, 10);
        return "cinema";
    }

    @PostMapping
    @ApiOperation("电影院分页列表")
    public String page(Model model, Cinema cinema, Integer hallTypeId, Integer pageNo, Integer pageSize) {
        //影院分页列表
        renderData(model, cinema, hallTypeId, pageNo, pageSize);
        return "cinema :: cinemaList";
    }
    private Page<CinemaVO> convert(Page<Cinema> cinemaPage){
        Page<CinemaVO> cinemaVOPage = new Page<>();
        BeanUtils.copyProperties(cinemaPage, cinemaVOPage);
        List<CinemaVO> cinemaVOList = cinemaPage.getRecords().stream().map(this::convert).collect(Collectors.toList());
        cinemaVOPage.setRecords(cinemaVOList);
        return cinemaVOPage;
    }
    private List<CinemaVO> convert(List<Cinema> cinemas){
        return cinemas.stream().map(this::convert).collect(Collectors.toList());
    }
    private CinemaVO convert(Cinema cinema) {
        CinemaVO cinemaVO = new CinemaVO();
        BeanUtils.copyProperties(cinema, cinemaVO);
        //查其下拥有的影厅类型
        List<String> hallTypeNames = hallService.listHallTypeNamesByCinemaId(cinema.getId());
        cinemaVO.setHallTypeNames(hallTypeNames);
        return cinemaVO;
    }
}
